//
//  HousingCell.m
//  RENTVISITOR
//
//  Created by wonder on 2017/9/7.
//  Copyright © 2017年 UNN. All rights reserved.
//

#import "HousingCell.h"
#import "RentVieitor.h"
#import "FangModel.h"

@implementation HousingCell {
    
    UIImageView *imgview;
    UILabel *introduce;
    UILabel *locationLabel;
    UILabel *describeLabel;
    UILabel *tagLabel;
    UILabel *tagLabel2;
    UILabel *tagLabel3;
    UILabel *price;
    UILabel *brokerName;
    UILabel *siteLabel;

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        //        self.contentView.backgroundColor = [UIColor redColor];
        self.selectionStyle = UITableViewCellEditingStyleNone;
        
        [self creatView];
    }
    return self;
}


- (void)creatView {
    
    __weak typeof(self) weakSelf = self;
    
    imgview = [[UIImageView alloc] init];
    imgview.image = [UIImage imageNamed:@"recomimg_01"];
    [self.contentView addSubview:imgview];
    
    [imgview mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(weakSelf.contentView.mas_top).with.offset(15);
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(15);
        make.size.mas_equalTo(CGSizeMake(110*Main_Scale, 80*Main_Scale));
    }];
    
    introduce = [[UILabel alloc] init];
    introduce.text = @"紧邻十五号地铁 家电齐全 双南向";
    introduce.numberOfLines = 0;
    introduce.font = [UIFont systemFontOfSize:15*Main_Scale];
    introduce.textColor = [Utils colorConvertFromString:@"#101010"];
    introduce.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:introduce];
    
    [introduce mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imgview.mas_right).with.offset(10);
        make.top.mas_equalTo(imgview.mas_top).with.offset(0);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(10);
        make.height.mas_equalTo(@(15*Main_Scale));
    }];
    
    UIImageView *location = [[UIImageView alloc] init];
    location.image = [UIImage imageNamed:@"位置"];
    [self.contentView addSubview:location];
    
    [location mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
        make.top.mas_equalTo(introduce.mas_bottom).with.offset(10*Main_Scale);
        make.size.mas_equalTo(CGSizeMake(12*Main_Scale, 12*Main_Scale));
    }];
    
    locationLabel = [[UILabel alloc] init];
    locationLabel.text = @"雅颂居";
    locationLabel.font = [UIFont systemFontOfSize:12*Main_Scale];
    locationLabel.textColor = [Utils colorConvertFromString:@"#101010"];
    locationLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:locationLabel];
    
    [locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(location.mas_right).with.offset(3);
        make.top.mas_equalTo(introduce.mas_bottom).with.offset(10*Main_Scale);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-10);
        make.height.mas_equalTo(12*Main_Scale);
    }];
    
    UIImageView *describe = [[UIImageView alloc] initWithFrame:CGRectMake(135, 56, 12, 12)];
    describe.image = [UIImage imageNamed:@"概况"];
    [self.contentView addSubview:describe];
    
    [describe mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
        make.top.mas_equalTo(locationLabel.mas_bottom).with.offset(10*Main_Scale);
        make.size.mas_equalTo(CGSizeMake(12*Main_Scale, 12*Main_Scale));
    }];
    
    
    describeLabel = [[UILabel alloc] initWithFrame:CGRectMake(155,56,KWidth-155 ,12)];
    describeLabel.text = @"2室一厅一卫|83m²|高层(共22层)|朝南";
    describeLabel.numberOfLines = 0;
    describeLabel.font = [UIFont systemFontOfSize:12];
    describeLabel.textColor = [Utils colorConvertFromString:@"#101010"];
    describeLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:describeLabel];
    
    [describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(describe.mas_right).with.offset(3);
        make.top.mas_equalTo(locationLabel.mas_bottom).with.offset(10*Main_Scale);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-3);
        make.height.mas_equalTo(12*Main_Scale);
    }];
    
    price = [[UILabel alloc] init];
    price.text = @"7800元/月";
    price.font = [UIFont systemFontOfSize:15];
    price.textColor = [Utils colorConvertFromString:@"#e52323"];
    price.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:price];
    
    [price mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
        make.top.mas_equalTo(describeLabel.mas_bottom).with.offset(10*Main_Scale);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-10);
        make.height.mas_equalTo(15*Main_Scale);
    }];
    
    
    
    siteLabel = [[UILabel alloc] init];
    siteLabel.text = @"深圳区南山区科技中三路13号";
    siteLabel.font = [UIFont systemFontOfSize:13];
    siteLabel.textColor = [Utils colorConvertFromString:@"#fc7f33"];
    siteLabel.backgroundColor = [Utils colorConvertFromString:@"#fff2ea"];
    siteLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:siteLabel];
    
    
    
    [siteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(30);
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-30);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(-10);
        make.height.mas_equalTo(@30);
        
    }];
}

- (void)setModel:(FangModel *)model  {
    
    _model = model;
    [imgview sd_setImageWithURL:[NSURL URLWithString:_model.image] placeholderImage:[UIImage imageNamed:@"未载入img"]];
    //    [imgview sd_setImageWithURL:[NSURL URLWithString:_model.image]];
    introduce.text = _model.title;
    price.text = [NSString stringWithFormat:@"%@元/月",_model.rent];
    locationLabel.text = _model.region_name;
    siteLabel.text = _model.address;
    NSString *str = [NSString stringWithFormat:@"%@室%@厅%@卫|%@m²|%@层|%@",_model.d_bedroom,_model.d_livingroom,_model.d_washroom,_model.d_square,_model.d_total_floor,_model.d_floor];
    describeLabel.text = str;
    NSString *mString = model.label;
    
    if (mString == nil) {
        mString = @"";
    }
    NSString *address = _model.address;
    if (address == nil) {
        address = @"";
    }
    if ([address isEqualToString:@""]) {
        
        [siteLabel removeFromSuperview];
    }
    
    //更新高度
    //    __weak typeof(self) weakSelf = self;
    //    if ([mString isEqualToString:@""]) {
    //
    //        [price mas_updateConstraints:^(MASConstraintMaker *make) {
    //
    //            make.left.mas_equalTo(imgview.mas_right).with.offset(10*Main_Scale);
    //            make.top.mas_equalTo(describeLabel.mas_bottom).with.offset(10*Main_Scale);
    //            make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-10);
    //            make.height.mas_equalTo(15*Main_Scale);
    //        }];
    //    }
    
    
    NSArray* array;
    if ([mString isEqualToString:@""] == NO) {
        
        mString = [mString stringByReplacingOccurrencesOfString:@"["withString:@""];
        mString = [mString stringByReplacingOccurrencesOfString:@"]"withString:@""];
        mString = [mString stringByReplacingOccurrencesOfString:@"\""withString:@""];
        array = [mString componentsSeparatedByString:@","];
    }
    
    
    if (array.count >=3) {
        
        tagLabel3.hidden = NO;
        tagLabel3.text = array[2];
    }
    
    if (array.count >=2) {
        
        tagLabel2.hidden = NO;
        tagLabel2.text = array[1];
    }
    if (array.count >=1) {
        if ([array[0] isEqualToString:@""]) {
            return;
        }
        tagLabel.hidden = NO;
        tagLabel.text = array[0];
    }
    
}
@end
